// 学生个人信息页面脚本

$(document).ready(function() {
    // 从URL参数获取学生ID
    const urlParams = new URLSearchParams(window.location.search);
    const studentId = urlParams.get('id');
    
    if (studentId) {
        loadStudentProfile(studentId);
    } else {
        showAlert('未指定学生ID', 'danger');
        setTimeout(() => {
            window.location.href = '/students';
        }, 2000);
    }
    
    // 绑定编辑按钮事件
    $('#editProfileBtn').click(function() {
        window.location.href = `/students?edit=${studentId}`;
    });
});

// 加载学生个人信息
function loadStudentProfile(studentId) {
    $.ajax({
        url: `/api/students/${studentId}`,
        type: 'GET',
        success: function(response) {
            if (response.code === 200) {
                renderStudentProfile(response.data);
            } else {
                showAlert(response.message, 'danger');
            }
        },
        error: function(xhr) {
            const response = JSON.parse(xhr.responseText || '{}');
            showAlert(response.message || '加载学生信息失败', 'danger');
        }
    });
}

// 渲染学生个人信息
function renderStudentProfile(student) {
    // 基本信息
    $('#studentName').text(student.name || '-');
    $('#studentId').text(student.id || '-');
    $('#studentMajor').text(student.major || '-');
    
    // 详细信息
    $('#profileName').text(student.name || '-');
    $('#profileGender').text(student.gender || '-');
    $('#profileBirthDate').text(student.birthDate || '-');
    $('#profileMajor').text(student.major || '-');
    $('#profileClassNumber').text(student.classNumber || '-');
    $('#profileEnrollmentDate').text(student.enrollmentDate || '-');
    $('#profileAddress').text(student.address || '-');
    $('#profilePhone').text(student.phone || '-');
    $('#profileEmail').text(student.email || '-');
    
    // 头像
    if (student.avatarPath) {
        $('#avatarImg').attr('src', student.avatarPath);
    }
    
    // 身份证件照片
    if (student.idCardPath) {
        $('#idCardSection').html(`
            <a href="${student.idCardPath}" target="_blank" class="file-link">
                <i class="file-icon">📄</i>
                查看身份证件照片
            </a>
        `);
    }
    
    // 其他附件
    if (student.attachments && student.attachments.length > 0) {
        const attachmentsHtml = student.attachments.map(attachment => `
            <a href="${attachment.path}" target="_blank" class="file-link">
                <i class="file-icon">📎</i>
                ${attachment.name}
            </a>
        `).join('');
        $('#attachmentsSection').html(attachmentsHtml);
    }
}

// 返回学生列表
function goBack() {
    window.location.href = '/students';
}

// 显示提示信息
function showAlert(message, type) {
    const alertHtml = `
        <div class="alert alert-${type} alert-dismissible fade show" role="alert">
            ${message}
            <button type="button" class="close" data-dismiss="alert">
                <span>&times;</span>
            </button>
        </div>
    `;
    
    // 如果页面中没有alertContainer，创建一个
    if ($('#alertContainer').length === 0) {
        $('body').prepend('<div id="alertContainer"></div>');
    }
    
    $('#alertContainer').html(alertHtml);
    
    // 3秒后自动隐藏
    setTimeout(() => {
        $('.alert').fadeOut();
    }, 3000);
}